import axios from "axios";
// @ts-ignore
import { ElMessage } from "element-plus";
//创建axios实例
let myRequest = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
  // withCredentials: true,
})
//请求拦截器
myRequest.interceptors.request.use(config => {
  // config.headers['token'] = localStorage.getItem('token') || '';
  return config;
});
//响应拦截器
myRequest.interceptors.response.use((response) => {
  return response.data;
}, (error) => {
  console.log(error)
  ElMessage.error(error.message)
  return Promise.reject(error);
});
const Request = (url, options = {}) => {
  let method = options.method || 'get';
  let params = options.params || {};
  
  if (method === 'get' || method === 'GET') {
    return new Promise((resolve, reject) => {
      myRequest
        .get(url, {
          params: params,
        })
        .then(res => {
          if (res) {
            resolve(res);
          }
        })
        .catch(err => {
          reject(err);
        });
    });
  } else {
    return new Promise((resolve, reject) => {
      myRequest
        .post(url, params)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};

export default Request;